package com.integral.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.integral.biz.IBalanceBiz;
import com.integral.biz.IBankBiz;
import com.integral.biz.IOrderBiz;
import com.integral.constant.OrderConstant;
import com.integral.entity.Balance;
import com.integral.entity.Order;
import com.integral.entity.UserInfo;
import com.integral.vo.OrderVo;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Resource
	private IOrderBiz orderBiz;
	
	@Resource
	private IBankBiz bankBiz;
	
	@Resource
	private IBalanceBiz balanceBiz;
	
	@RequestMapping("/findOrder")
	@ResponseBody
	public Map<String, Object> findOrder(OrderVo orderVo){
		//设置查询未删除的数据
		orderVo.setDeleteFlag(0);
		
		List<OrderVo> list = orderBiz.findOrder(orderVo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", orderBiz.findOrderCount(orderVo));
		map.put("rows", list);
		return map;
	}
	/**
	 * 驳回
	 * @return
	 */
	@RequestMapping(value="/auditFileOrder")
	@ResponseBody
	public int auditFileOrder(String orderNo,String userCode,BigDecimal money,HttpServletRequest request){
		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
		Order order = new Order();
		order.setOrderNo(orderNo);
		order.setOrderStatus(OrderConstant.ORDER_STATUS_FAIL);
		order.setCreateTime(new Date());
		order.setCreateEmp(userInfo.getUserName());
		orderBiz.updateByStatus(order);
		//修改订单余额
        Balance balance = balanceBiz.findByUserCode(userCode);
        if(null != balance){
        	balance.setUserMoney(balance.getUserMoney().add(money));
        	balanceBiz.updateBalance(balance);
        }
		return 0;
	}
	
	
	@RequestMapping("/addOrder")
	@ResponseBody
	public int addOrder(){
		Order order = new Order();
		//银行编码
		order.setBankCode("123");
		//银行产品
		order.setBankTypeCode("123");
		//用户编码
		order.setUserCode("bd123456781");
		//兑换积分
		order.setExchangeIntegral(333);
		//新增订单
		return orderBiz.addOrder(order);
		
//		Bank bank = new Bank();
//		bank.setBankCode("123");
//		bank.setBankExchange("aaaaa");
//		bank.setBankName("123123");
//		return bankBiz.addBank(bank);
	}
}
